﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.ComponentModel;

namespace UBlockly.Scriptable
{
    /// <summary>
    /// 函数返回值类型
    /// </summary>
    public enum ILVarType
    {
        None,
        [Description("int")] IntR,
        [Description("float")] FloatR,
        [Description("string")] StringR,
        [Description("void")] VoidR,
        [Description("bool")] BoolR,
        [Description("BlockEnum")] EnumR,
    }

    public interface IVarObject
    {
        ILVarType VarType { get; }
        object TargetValue { get; }
    }

    //类型转成str 和 str转成类型
    internal static class IVarObjectHelper
    {
        public static IVarObject FromStr(string str)
        {
            var pp = str.Split('&');
            if(pp.Length != 2)
                throw new SystemException($"IVarObject FromStr {str} error");
            if (pp[0].StartsWith("IntR"))
            {
                IntR target = new IntR();
                target.Value = int.Parse(pp[1]);
                return target;
            }
            if(pp[0].StartsWith("FloatR"))
            {
                FloatR target = new FloatR();
                target.Value = float.Parse(pp[1]);
                return target;
            }
            if(pp[0].StartsWith("StringR"))
            {
                StringR target = new StringR();
                target.Value = pp[1];
                return target;
            }
            if(pp[0].StartsWith("BoolR"))
            {
                BoolR target = new BoolR();
                target.Value = bool.Parse(pp[1]);
                return target;
            }
            if(pp[0].StartsWith("EnumR"))
            {
                EnumR target = new EnumR();
                //target.Value = Enum.Parse(pp[1]);
                return target;
            }
            throw new SystemException($"IVarObject FromStr {str} error");
        }

        public static string ToStr(IVarObject obj)
        {
            if (obj is IntR)
            {
                return $"IntR&{((IntR)obj).Value}";
            }

            if (obj is FloatR)
            {
                return $"FloatR&{((FloatR)obj).Value}";
            }

            if (obj is StringR)
            {
                return $"StringR&{((StringR)obj).Value}";
            }

            if (obj is BoolR)
            {
                return $"BoolR&{((BoolR)obj).Value}";
            }

            if (obj is EnumR)
            {
                return $"EnumR&{((EnumR)obj).Value}";
            }
            throw new SystemException($"IVarObject ToStr {obj.VarType} error");
        }

        public static string ToDisplayStr(IVarObject obj)
        {
            var str = ToStr(obj);
            return str.Replace(str.Substring(0, str.IndexOf('&') + 1), "");
        }
    }

    public class VoidR : IVarObject
    {
        private System.Action continueCallback;
        public ILVarType VarType => ILVarType.VoidR;
        public object TargetValue => null;

        public System.Action ContinueCallback => ContinueRun;

        public void SetConitnue(System.Action continueCall)
        {
            continueCallback = continueCall;
        }

        public static VoidR Default()
        {
            return new VoidR();
        }

        void ContinueRun()
        {
            //在下一zhen继续执行逻辑，使其它逻辑可以插入
            CoroutineFuntion.DelayAction(0, () => { continueCallback?.Invoke(); });
        }
    }

    public class IntR : IVarObject
    {
        public int Value;
        public ILVarType VarType => ILVarType.IntR;
        public object TargetValue => Value;
    }

    public class FloatR : IVarObject
    {
        public float Value;
        public ILVarType VarType => ILVarType.FloatR;
        public object TargetValue => Value;
    }

    public class BoolR : IVarObject
    {
        public bool Value;
        public ILVarType VarType => ILVarType.BoolR;
        public object TargetValue => Value;
    }

    public class EnumR : IVarObject
    {
        public System.Type EnumType;
        public System.Enum Value;
        public ILVarType VarType => ILVarType.EnumR;
        public object TargetValue => Value;
    }

    public class StringR : IVarObject
    {
        public string Value;
        public ILVarType VarType => ILVarType.StringR;
        public object TargetValue => Value;
    }

    public class VoidR1 : VoidR
    {
        
    }

    public class VoidR2 : VoidR
    {
    }

    public class IntR1 : IntR
    {
    }

    public class IntR2 : IntR
    {
    }

    public class IntR3 : IntR
    {
    }

    public class IntR4 : IntR
    {
    }

    public class IntR5 : IntR
    {
    }

    public class IntR6 : IntR
    {
    }

    public class IntR7 : IntR
    {
    }

    public class IntR8 : IntR
    {
    }

    public class IntR9 : IntR
    {
    }

    public class BoolR1 : BoolR
    {
    }

    public class BoolR2 : BoolR
    {
    }

    public class BoolR3 : BoolR
    {
    }

    public class BoolR4 : BoolR
    {
    }

    public class Bool5 : BoolR
    {
    }

    public class BoolR6 : BoolR
    {
    }

    public class BoolR7 : BoolR
    {
    }

    public class BoolR8 : BoolR
    {
    }

    public class BoolR9 : BoolR
    {
    }

    public class StringR1 : StringR
    {
    }

    public class StringR2 : StringR
    {
    }

    public class StringR3 : StringR
    {
    }

    public class StringR4 : StringR
    {
    }

    public class StringR5 : StringR
    {
    }

    public class StringR6 : StringR
    {
    }

    public class StringR7 : StringR
    {
    }

    public class StringR8 : StringR
    {
    }

    public class StringR9 : StringR
    {
    }

    public class FloatR1 : FloatR
    {
    }

    public class FloatR2 : FloatR
    {
    }

    public class FloatR3 : FloatR
    {
    }

    public class FloatR4 : FloatR
    {
    }

    public class FloatR5 : FloatR
    {
    }

    public class FloatR6 : FloatR
    {
    }

    public class FloatR7 : FloatR
    {
    }

    public class FloatR8 : FloatR
    {
    }

    public class FloatR9 : FloatR
    {
    }
    
    public class EnumR1 : EnumR
    {
    }

    public class EnumR2 : EnumR
    {
    }

    public class EnumR3 : EnumR
    {
    }

    public class EnumR4 : EnumR
    {
    }

    public class EnumR5 : EnumR
    {
    }

    public class EnumR6 : EnumR
    {
    }

    public class EnumR7 : EnumR
    {
    }

    public class EnumR8 : EnumR
    {
    }

    public class EnumR9 : EnumR
    {
    }
    
}